package com.igame.Controller;

import com.github.pagehelper.Page;
import com.igame.Service.CategoriesService;
import com.igame.Service.GoodsImgService;
import com.igame.Service.GoodsService;
import com.igame.common.PageResult;
import com.igame.common.Result;
import com.igame.dto.DeleteCarGoodsDao;
import com.igame.dto.GoodsDao;
import com.igame.dto.GoodsPageQueryDto;

import com.igame.dto.HistoryDto;
import com.igame.entity.*;
import com.igame.vo.GoodsVo;
import com.igame.vo.ShoppingCartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;


@CrossOrigin //忽视跨域问题
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsImgService goodsImgService;

    @Autowired
    private CategoriesService categoriesService;
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody GoodsPageQueryDto goodsPageQueryDto) {
        String name = goodsPageQueryDto.getName();
        PageResult pageResult = goodsService.pageQuery(goodsPageQueryDto, name);
        return Result.success(pageResult);
    }
    @PostMapping("/allGoods")
    public Result<List<Goods>> allGoods(@RequestBody GoodsDao goodsDao){
        return goodsService.goodsList(goodsDao);
    }

    @PostMapping("/getGoodsById")
    public Result<GoodsVo> getGoodsById(@RequestBody Integer id){
        return goodsService.getGoodsById(id);
    }

    @PostMapping("/getGoodsImg")
    public  Result<List<GoodsImg>> getGoodsImg(@RequestBody Integer id){
        return goodsImgService.getGoodsImg(id);
    }

    @PostMapping("/getCategoriesNameById")
    public Result<String> getNameById(@RequestBody Integer id){
        return categoriesService.getNameById(id);
    }

    @PostMapping("/addToCar")
    public Result insertToCar(@RequestBody ShoppingCart shoppingCart){
        return goodsService.insertToCar(shoppingCart);
    }

    @PostMapping("/getCarGoods")
    public Result<List<ShoppingCartVo>> getCarGoods(@RequestBody Integer userId){
       return goodsService.getCarGoods(userId);
    }

    @PostMapping("/deleteCarGoods")
    public Result deleteCarGoods(@RequestBody DeleteCarGoodsDao dao){
        return goodsService.deleteCarGoods(dao);
    }

    @PostMapping("/getIPosted")
    public Result<List<GoodsVo>> getIPosted(@RequestBody Integer id){
       return goodsService.getIPosted(id);
    }

    @PostMapping("/iSold")
    public Result<List<GoodsVo>> iSold(@RequestBody Integer id){
        return goodsService.iSold(id);
    }
//商品id来自用户的购物车
    @PostMapping("/getIds")
    public Result<List<GoodsVo>> getIdFormBy(@RequestBody Integer[] ids){
        return goodsService.buyFormCar(ids);
    }
//来自单独购买
    @PostMapping("/getId")
    public Result<GoodsVo> soloBuy(@RequestBody Integer id){
        return goodsService.soloBuy(id);
    }

//    未付款商品
    @PostMapping("/getObligation")
    public Result<List<GoodsVo>> getObligation(@RequestBody Integer loginId){
        return  goodsService.selectObligation(loginId);
    }
//   待收货
    @PostMapping("/getPendingReceipt")
    public Result<List<GoodsVo>> pendingReceipt(@RequestBody Integer loginId){
        return  goodsService.selectPendingReceipt(loginId);
    }
//    已完成
    @PostMapping("/getCompleted")
    public Result<List<GoodsVo>> getCompleted(@RequestBody Integer loginId){
        return  goodsService.getCompleted(loginId);
    }
// 接收商品id存储历史查看
    @PostMapping("/sendGoodsId")
    public void sendGoodsId(@RequestBody HistoryDto historyDto){
        goodsService.sendGoodsIdToRedis(historyDto);
    }

    @PostMapping("/getHistory")
    public Result<List<GoodsVo>> getHistory(@RequestBody Integer userId){
       return goodsService.getHistory(userId);
    }

    @GetMapping("/getAllFreePost")
    public Result<List<FreePost>> getAllFreePost(){
        return goodsService.getAllFreePost();
    }

    @GetMapping("/getAllPage")
    public Result<List<Pack>> getAllPage(){
        return goodsService.getAllPack();
    }

    @GetMapping("/getAllFineness")
    public Result<List<Fineness>> getAllFineness(){
        return goodsService.getAllFineness();
    }

    @GetMapping("/getAllVersion")
    public Result<List<Version>> getAllVersion(){
        return goodsService.getAllVersion();
    }

    @GetMapping("/getAllPlatform")
    public Result<List<Platform>> getAllPlatform(){
        return goodsService.getAllPlatform();
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("name") String name,
                       @RequestParam("description") String description,
                       @RequestParam("price") BigDecimal price,
                       @RequestParam("categoryId") Integer categoryId,
                       @RequestParam("freePostId") Integer freePostId,
                       @RequestParam("pageOptionId") Integer pageOptionId,
                       @RequestParam("conditionId") Integer conditionId,
                       @RequestParam("versionId") Integer versionId,
                       @RequestParam("platformId") Integer platformId,
                       @RequestParam("introduction") String introduction,
                       @RequestParam("loginId") Integer loginId,
                       @RequestPart(value = "coverImage", required = false) MultipartFile coverImage,
                       @RequestPart(value = "images", required = false) List<MultipartFile> images) {

        return goodsService.addGoods(name,description,price,categoryId,freePostId,pageOptionId,conditionId,versionId,platformId,introduction,loginId,coverImage,images);
    }
}

